package com.quectel.emp.controller.scheduling;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.business.common.scheduling.SchedulingLeaveBusiness;
import com.quectel.business.common.util.sys.SysCodeUtils;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.sys.SysCodeKeyConstants;
import com.quectel.core.module.scheduling.dto.SchedulingLeaveConfigDto;
import com.quectel.core.module.scheduling.dto.SchedulingLeavePersonBindingDto;
import com.quectel.core.module.scheduling.service.SchedulingLeaveBalanceService;
import com.quectel.core.module.scheduling.service.SchedulingLeaveConfigService;
import com.quectel.core.module.scheduling.service.SchedulingLeavePersonBindingService;
import com.quectel.core.module.schedulingapply.dto.vo.SchedulingTimeVO;
import com.quectel.emp.controller.BaseController;
import com.quectel.emp.util.SessionHolder;
import com.quectel.util.common.DateUtils;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/7/11 13:06
 * @description 考勤假期列表接口
 */
@RestController
@RequestMapping("schedulingLeaveConfig")
@Tag(name = "SchedulingLeaveConfigController", description = "考勤假期相关api")
public class SchedulingLeaveConfigController extends BaseController {


    @DubboReference
    private SchedulingLeaveConfigService schedulingLeaveConfigService;
    @DubboReference
    private SchedulingLeavePersonBindingService schedulingLeavePersonBindingService;
    @DubboReference
    private SchedulingLeaveBalanceService schedulingLeaveBalanceService;


    @Autowired
    private SchedulingLeaveBusiness schedulingLeaveBusiness;

    /**
     * 员工假期列表
     *
     * @return
     */
    @GetMapping("leaveList")
    @Operation(summary = "员工假期列表")
    public Response<List<SchedulingLeaveConfigDto>> leaveList(
            @Parameter(description = "是否限额,0不限额1限额") @RequestParam(required = false) Byte limitFlag
    ) {
        SessionHolder.checkEmp();

        Request request = Request.configParamsNotPage(Request.parseParams(httpServletRequest));

        //查询所有的假期类型
        request.put(LambdaUtil.getFieldName(SchedulingLeavePersonBindingDto::getDeptPersonId), SessionHolder.getEmp().getId());
        request.put(LambdaUtil.getFieldName(SchedulingLeaveConfigDto::getTenantId), SessionHolder.getEmp().getTenantId());
        request.put(LambdaUtil.getFieldName(SchedulingLeaveConfigDto::getVillageId), SessionHolder.getEmp().getVillageId());
        request.put(LambdaUtil.getFieldName(SchedulingLeaveConfigDto::getStatus), SystemConstants.YES);
        if (limitFlag != null) {
            request.put(LambdaUtil.getFieldName(SchedulingLeaveConfigDto::getLimitFlag), limitFlag);
        }

        List<SchedulingLeavePersonBindingDto> schedulingLeavePersonBindingDtos = schedulingLeavePersonBindingService.queryLeaveList(request.getParams());

        List<SchedulingLeaveConfigDto> collect = schedulingLeavePersonBindingDtos
                .stream()
                .map(schedulingLeavePersonBindingDto ->
                        {
                            SchedulingLeaveConfigDto schedulingLeaveConfigDto = schedulingLeaveConfigService.selectCacheById(schedulingLeavePersonBindingDto.getLeaveId());
                            schedulingLeaveConfigDto.setTypeName(SysCodeUtils.easyToGetName(SysCodeKeyConstants.SCHEDULING_LEAVE_TYPE, schedulingLeaveConfigDto.getType().toString()));
                            return schedulingLeaveConfigDto;
                        }

                )
                .filter(Objects::nonNull).collect(Collectors.toList());

        return Response.<List<SchedulingLeaveConfigDto>>ok().wrap(collect);
    }


    /**
     * 员工假期列表
     *
     * @return
     */
    @GetMapping("leaveBalance")
    @Operation(summary = "查询假期余额")
    public Response<SchedulingTimeVO> leaveBalance(
            @Parameter(description = "假期Id") @RequestParam Long leaveId,
            @Parameter(description = "请假开始时间,'yyyy-MM-dd HH:mm'") @RequestParam String startTime,
            @Parameter(description = "请假结束时间,'yyyy-MM-dd HH:mm'") @RequestParam String endTime

    ) {
        SessionHolder.checkEmp();

        Response<SchedulingTimeVO> leaveBalanceResult = schedulingLeaveBusiness.getLeaveBalance(leaveId, SessionHolder.getEmp().getId(),startTime,endTime);
        if (!leaveBalanceResult.isSuccess()) {
            return Response.error(leaveBalanceResult.getMessage());
        }

        return Response.<SchedulingTimeVO>ok().wrap(leaveBalanceResult.getData());
    }

    /**
     * 计算请假时长
     *
     * @return
     */
    @GetMapping("calLeaveTimeLength")
    @Operation(summary = "计算请假时长")
    public Response<SchedulingTimeVO> leaveTimeLength(
            @Parameter(description = "假期Id") @RequestParam Long leaveId,
            @Parameter(description = "开始时间") @RequestParam String startTime,
            @Parameter(description = "结束时间") @RequestParam String endTime
    ) {
        SessionHolder.checkEmp();
        SchedulingLeaveConfigDto schedulingLeaveConfigDto = schedulingLeaveConfigService.selectCacheById(leaveId);
        Response response = schedulingLeaveBusiness.checkLeaveConfig(schedulingLeaveConfigDto);
        if (!response.isSuccess()) {
            return Response.error(response.getMessage());
        }

        Date startDate = DateUtils.parse(startTime, DateUtils.FormatType.COMMON);
        Date endDate = DateUtils.parse(endTime, DateUtils.FormatType.COMMON);

        SchedulingTimeVO leaveApplicationTimeLength = schedulingLeaveBusiness.getLeaveApplicationTimeLength(
                schedulingLeaveConfigDto.getMinUnit(),
                schedulingLeaveConfigDto.getCalTimeLengthType(),
                startDate,
                endDate
        );
        return Response.<SchedulingTimeVO>ok().wrap(leaveApplicationTimeLength);
    }


}
